<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="策略id" prop="strategyId">
        <el-input
          v-model="queryParams.strategyId"
          placeholder="请输入策略id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型id" prop="typeId">
        <el-input
          v-model="queryParams.typeId"
          placeholder="请输入类型id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['strategyType:strategyType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['strategyType:strategyType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['strategyType:strategyType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="strategyTypeList" @selection-change="handleSelectionChange">
      <el-table-column label="策略id" align="center" prop="strategy.id" />
      <el-table-column label="类型id" align="center" prop="type.id" />
      <el-table-column label="策略名称" align="center" prop="strategy.name" />
      <el-table-column label="类型名称" align="center" prop="type.name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-hasPermi="['strategyType:strategyType:edit']">不支持修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row.strategy.id,scope.row.type.id)" v-hasPermi="['strategyType:strategyType:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改策略和类型的中间表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="strategyTypeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="策略名称" prop="strategyId">
          <el-select v-model="form.strategyId" placeholder="请选择策略名称">
            <el-option
              v-for="strategy in strategyOptions"
              :key="strategy.id"
              :label="strategy.name"
              :value="strategy.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型名称" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择类型名称">
            <el-option
              v-for="type in typeOptions"
              :key="type.id"
              :label="type.name"
              :value="type.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm()">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="StrategyType">
import { listStrategyType, getStrategyType, delStrategyType, addStrategyType, updateStrategyType } from "@/api/strategyType/strategyType";
import { listStrategy } from "@/api/strategy/strategy";
import { listType } from "@/api/type/type";

const { proxy } = getCurrentInstance();

const strategyTypeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    strategyId: null,
    typeId: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

// 存储策略下拉框选项数据
const strategyOptions = ref([]);
// 存储类型下拉框选项数据
const typeOptions = ref([]);

// 获取策略下拉框选项数据
const getStrategyOptions = async () => {
  const response = await listStrategy();
  if (response.code === 200) {
    strategyOptions.value = response.rows.map(item => ({
      id: item.strategy.id,
      name: item.strategy.name
    }));
  }
};

// 获取类型下拉框选项数据
const getTypeOptions = async () => {
  const response = await listType();
  if (response.code === 200) {
    typeOptions.value = response.rows.map(item => ({
      id: item.type.id,
      name: item.type.name
    }));
  }
};

// 在页面加载时获取下拉框选项数据
onMounted(async () => {
  await getStrategyOptions();
  await getTypeOptions();
});

/** 查询策略和类型的中间表列表 */
function getList() {
  loading.value = true;
  listStrategyType(queryParams.value).then(response => {
    strategyTypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    strategyId: null,
    typeId: null,
    isDeleted: null
  };
  proxy.resetForm("strategyTypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.strategyId);
  single.value = selection.length!= 1;
  multiple.value =!selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加策略和类型的中间表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _strategyId = row.strategyId || ids.value
  getStrategyType(_strategyId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改策略和类型的中间表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["strategyTypeRef"].validate(valid => {
    if (valid) {
      const strategyTypeObject = {
        strategyId: form.value.strategyId,
        typeId: form.value.typeId
      };
      if (form.value.strategyId == null) {
        updateStrategyType(strategyTypeObject).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStrategyType(strategyTypeObject).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleDelete(row1,row2) {
  console.log("===============")
  console.log(row1)
  console.log(row2)
  const _strategyIds = row1
  const _typeIds = row2
  proxy.$modal.confirm('是否确认删除策略和类型的中间表编号为"' + row1 + '"的数据项？').then(function () {
    return delStrategyType(row1 ,row2);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('strategyType/strategyType/export', {
  ...queryParams.value
  }, `strategyType_${new Date().getTime()}.xlsx`)
}

getList();
</script>